<?php

namespace Mobile\Controller;

class UserController extends CommonController
{
    /**
     * 会员主页
     */
    public function index()
    {
        $uid = session('ytata.uid');
        $userinfo = D('User')->get_user_one($uid, true);
        if (empty($userinfo)) {
            $userinfo['nickname'] = '[未登录]';
            $userinfo['avatar'] = __ROOT__ . '/' . C('YTATA_IMG_AVATAR_DIR') . 'avatar.jpg';
            $userinfo['openid'] = $openid;
            $userinfo['exp'] = 0;
            $userinfo['exp_lv'] = 1;
            $userinfo['mobile'] = '';
        }
        $lv = D('Lv')->get_lv_one($userinfo['exp_lv']);
        $userinfo['percentage'] = D('User')->get_exp_percentage($userinfo['exp'], $lv['exp_max']);
        if (empty($userinfo['percentage'])) {
            $userinfo['percentage'] = '20%';
        }
        $this->assign('pagemark', 'user');
        $this->assign('lv', $lv);
        $this->assign('userinfo', $userinfo);

        $this->display();
    }

    /**
     * 我的问答
     */
    public function question()
    {
        $this->check_login();
        $this->assign('uid', session('ytata.uid'));
        $this->assign('pagemark', 'user');
        $this->display();
    }

    /**
     * 问答列表
     */
    public function question_list()
    {
        $this->check_login();
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $this->assign('uid', session('ytata.uid'));
        $html = $this->fetch('question_list');
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html, 'loadurl' => query_url('question_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 删除问题
     */
    public function question_del()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $id = I('get.id', '0', 'intval');
        //获取标签
        $tagmap['qid'] = $id;
        $tags = M('QuestionTag')->where($tagmap)->select();
        foreach ($tags as $t) {
            $set_tag[] = $t['tagid'];
        }
        D('Question')->del_question($id, $uid);
        //更新标签统计
        D('Tag')->set_tag_count($set_tag, 'count_question');
        $this->ajaxReturn(1, '删除成功！');

    }

    /**
     * 我的回答
     */
    public function question_reply()
    {
        $this->check_login();
        $this->assign('uid', session('ytata.uid'));
        $this->assign('pagemark', 'user');
        $this->display();
    }

    /**
     * 问题回复列表
     */
    public function question_reply_list()
    {
        $this->check_login();
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $this->assign('uid', session('ytata.uid'));
        $html = $this->fetch('question_reply_list');
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html, 'loadurl' => query_url('question_reply_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 问题回复删除
     */
    public function question_reply_del()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $id = I('get.id', '0', 'intval');
        $ret = D('Answer')->del_answer($id, $uid);
        if ($ret['status']) {
            $this->ajaxReturn(1, $ret['content']);
        } else {
            $this->ajaxReturn(0, $ret['content']);
        }
    }

    /**
     * 我的主题
     */
    public function bbs()
    {
        $this->check_login();
        $this->assign('uid', session('ytata.uid'));
        $this->assign('pagemark', 'user');
        $this->display();
    }

    /**
     * 主题列表
     */
    public function bbs_list()
    {
        $this->check_login();
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $this->assign('uid', session('ytata.uid'));
        $html = $this->fetch();
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html, 'loadurl' => query_url('bbs_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 删除主题
     */
    public function bbs_del()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $id = I('get.id', '0', 'intval');
        D('Bbs')->del_bbs($id, $uid);
        $this->ajaxReturn(1, '删除成功！');
    }

    /**
     * 主题回复
     */
    public function bbs_reply()
    {
        $this->check_login();
        $this->assign('uid', session('ytata.uid'));
        $this->assign('pagemark', 'user');
        $this->display();
    }

    /**
     * 主题回复列表
     */
    public function bbs_reply_list()
    {
        $this->check_login();
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $this->assign('uid', session('ytata.uid'));
        $html = $this->fetch();
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html, 'loadurl' => query_url('bbs_reply_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 主题回复删除
     */
    public function bbs_reply_del()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $id = I('get.id', '0', 'intval');
        $ret = D('BbsReply')->del_reply($id, $uid);
        $this->ajaxReturn(1, '删除成功');
    }

    /**
     * 文章
     */
    public function article()
    {
        $this->check_login();
        $this->assign('uid', session('ytata.uid'));
        $this->assign('pagemark', 'user');
        $this->display();
    }

    /**
     * 文章列表
     */
    public function article_list()
    {
        $this->check_login();
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $this->assign('uid', session('ytata.uid'));
        $html = $this->fetch();
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html, 'loadurl' => query_url('article_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 文章删除
     */
    public function article_del()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $id = I('get.id', '0', 'intval');
        $ret = D('Article')->del_article($id, $uid);
        $this->ajaxReturn(1, '删除成功');
    }

    /**
     * 任务
     */
    public function task()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $this->assign('uid', $uid);
        $map['is_display'] = 1;
        $list = D('Task')->get_list($map, true, $uid);
        $this->assign('list', $list);
        $this->assign('pagemark', 'user');
        $this->display();
    }

    /**
     * 设置
     */
    public function set()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $this->assign('uid', $uid);
        $this->assign('pagemark', 'user');
        $this->display();

    }

    /**
     * 金币记录
     */
    public function set_gold()
    {
        $this->check_login();
        $this->assign('pagemark', 'user');
        $uid = session('ytata.uid');
        $map['uid'] = $uid;
        $map['type'] = 1;
        $this->assign('list', D('LogGold')->log_list($map));
        $userinfo = D('User')->get_user_one($uid, true);
        $this->assign('userinfo', $userinfo);
        $this->display();
    }

    /**
     * 经验记录
     */
    public function set_exp()
    {
        $this->check_login();
        $this->assign('pagemark', 'user');
        $uid = session('ytata.uid');
        $map['uid'] = $uid;
        $map['type'] = 2;//2是经验变更日志
        $this->assign('list', D('LogGold')->log_list($map));
        $userinfo = D('User')->get_user_one($uid, true);
        $this->assign('userinfo', $userinfo);
        $this->display();
    }

    /**
     * 登录日志
     */
    public function set_loginlog()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $this->assign('pagemark', 'user');
        $this->assign('list', D('LogLogin')->log_list($uid));
        $this->display();
    }

    /**
     * 我的等级
     */
    public function set_lv()
    {
        $this->check_login();
        $this->assign('pagemark', 'user');
        $list = D("Lv")->get_lv_list();
        $uid = session('ytata.uid');
        $userinfo = D('User')->get_user_one($uid, true);
        $this->assign('userinfo', $userinfo);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 微信模板通知开关
     */
    public function set_remind()
    {
        $this->check_login();
        $this->assign('pagemark', 'user');
        $uid = session('ytata.uid');
        $userinfo = D('User')->get_user_one($uid, true);
        $this->assign('userinfo', $userinfo);
        $this->display();
    }

    /**
     * 微信模板通知开关保存
     */
    public function set_remind_save()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $isremind = I('request.isremind', '1', 'intval');
        $map['isremind'] = $isremind;
        D('User')->where(array('uid', $uid))->save($map);
        $this->ajaxReturn(1, '修改成功！');
    }

    /**
     * 用户登出
     */
    public function ajax_login_out()
    {
        D('User')->clear_session();
        $this->ajaxReturn(1, '退出成功！');
    }
}

?>